package com.votereport.android;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.votereport.android.ui.HorizontalSlider;
import com.votereport.android.ui.HorizontalSlider.OnProgressChangeListener;

/*
 * This is the user interface activity for the report form
 */
public class ReportFormActivity extends Activity implements OnClickListener, OnProgressChangeListener, Runnable
{

	private final String TAG = "reportForm";
	
	private ProgressDialog progressDialog = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        setContentView(R.layout.reportform); 
       
        setupSpinner();
        
        HorizontalSlider slider = (HorizontalSlider) this.findViewById(R.id.slider);
        slider.setProgress(50);
		slider.setOnProgressChangeListener(this);
		
      ((Button)findViewById(R.id.btnReportFormSubmit)).setOnClickListener(this);
      ((Button)findViewById(R.id.btnReportFormCancel)).setOnClickListener(this);
      
      
      TextView tvLocation = (TextView)findViewById(R.id.labelLocation);
      
      String txtLocation = "Your current location: " + LocationFinderActivity.getCurrentLocationName();
      
      tvLocation.setText(txtLocation);
      
    }
    
    private void setupSpinner ()
    {
    	  Spinner spinner = (Spinner)findViewById(R.id.spinnerWaitTime);
    	  
          ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
              android.R.layout.simple_spinner_dropdown_item,
                  new String[] {  "No Wait Time","Less Than 5 Minutes","5-15 Minute Wait","15-30 Minute Wait","30-45 Minute Wait","45-60 Minute Wait","Over 1 Hour Wait","Over 2 Hour Wait" });
			 
    	  spinner.setAdapter(spinnerArrayAdapter);

    

    }
    
    

	@Override
	public void onClick(View v) {
		
		if (v.getId()==R.id.btnReportFormSubmit)
		{
			progressDialog = ProgressDialog.show(ReportFormActivity.this,
				      "Submitting Report",
				      "Please wait...",
				      true);
				
			Handler handler = new Handler();
			
			handler.postDelayed(this, 1000);
			
		}
		else if (v.getId()==R.id.btnReportFormCancel)
		{
			showMain ();
		}
	}
	
	
	public void run ()
	{
		
		
		submitForm ();
		
	}
	
	private void submitForm ()
    {
    	try
        {
    	   Log.i(TAG, "submitting form...");
    	   
 	   //    Location location = 
 	       Location location = LocationFinderActivity.getCurrentLocation();
 	       
 	       String locationName = Reporter.getLocationName(location);
 	       String waitTime = (String)((Spinner)findViewById(R.id.spinnerWaitTime)).getSelectedItem();
 	       String rating = ((HorizontalSlider)findViewById(R.id.slider)).getSliderValue() + "";
 	       String comment = ((TextView)findViewById(R.id.entryComments)).getText().toString();
 	       
 	       String reporterName = ((TextView)findViewById(R.id.entryName)).getText().toString();
 	       String pollingPlace = ((TextView)findViewById(R.id.entryPollingPlace)).getText().toString();
 	       
 	      String tags = "";
 	      
 	      if (((CheckBox)findViewById(R.id.cbVotingMachine)).isChecked())
 	    	  tags += "#machine ";
 	      
 	     if (((CheckBox)findViewById(R.id.cbBallots)).isChecked())
	    	  tags += "#ballots ";
 	     
	 	    if (((CheckBox)findViewById(R.id.cbRegProcess)).isChecked())
		    	  tags += "#registration ";
	 	    
	 	   if (((CheckBox)findViewById(R.id.cbWidespread)).isChecked())
		    	  tags += "#challenges ";
	 	   
	 	  if (((CheckBox)findViewById(R.id.cbAccessibility)).isChecked())
		    	  tags += "#hava ";
 	      
	 	  comment += " " + waitTime + " ";
	 	  comment += " " + waitTime + " ";
 	      comment += tags;
 	      
 	       boolean reportAccepted = Reporter.submitReport(reporterName, pollingPlace, locationName, location, waitTime, rating, comment, tags);
 	       
 	       if (progressDialog != null)
 	    	  progressDialog.dismiss();
 	       
 	       if (reportAccepted)
 	       {
 	    	   Toast.makeText(getBaseContext(), "Thank you. Your report has been accepted!", Toast.LENGTH_LONG).show();

 	    	   showMain();
 	    	   
 	       }
 	       else
 	       {
 	    	   Toast.makeText(getBaseContext(), "There was a problem submitting your report. Wait a second, and then try again!", Toast.LENGTH_LONG).show();
 	       }
 	      
        }
        catch (Exception e)
        {
     	   e.printStackTrace();
        }
    }

	@Override
	public void onProgressChanged(View v, int progress) {
		
		((TextView)findViewById(R.id.sliderValue)).setText("" + progress);
		
	}
	
	private void showMain ()
	{
		Intent iMain = new Intent(this, LocationFinderActivity.class);
         
         startActivity(iMain);
	}
    
}
